package cn.com.smartdevices.bracelet.chart;

import android.animation.Animator;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.animation.AccelerateDecelerateInterpolator;

import com.goodving.guli.bt.model.PersonInfo;
import com.goodving.guli.bt.profile.scale.GuliLog;
import com.goodving.guli.view.C4220a;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import cn.com.smartdevices.bracelet.chart.base.BarChart.BarItem;
import cn.com.smartdevices.bracelet.chart.base.BaseChartView;
import cn.com.smartdevices.bracelet.chart.base.C0759p;
import cn.com.smartdevices.bracelet.chart.base.ChartScroller;
import cn.com.smartdevices.bracelet.chart.base.Renderer;
import cn.com.smartdevices.bracelet.chart.data.ChartDataLoader;
import cn.com.smartdevices.bracelet.chart.data.ItemData;


public class StatisticChartView extends BaseChartView<StatisticChart> {
    public static final int f3908a = 0;
    public static final int f3909b = 1;
    public static final int f3910c = 2;
    public static final int f3911d = 3;
    private static final String f3912q = "Chart.StatisticChartView";
    private boolean f3913A;
    private float f3914B;
    private float f3915C;
    private float f3916D;
    private float f3917E;
    private int f3918F;
    private int f3919G;
    private boolean f3920H;
    private ChartScroller.ScrollingListener f3921I;
    private Renderer f3922r;
    private Renderer f3923s;
    private List<StatisticChart.StatisticBarItem> f3924t;
    private List<StatisticChart.StatisticBarItem> f3925u;
    private List<C0730H> f3926v;
    private List<StatisticChart.DateItem> f3927w;
    private int f3928x;
    private ArrayList<Integer> f3929y;
    private ChartDataLoader.LoadCallback f3930z;

    class C0736K implements ChartScroller.ScrollingListener {
        final /* synthetic */ StatisticChartView f3896a;

        C0736K(StatisticChartView statisticChartView) {
            this.f3896a = statisticChartView;
        }

        public void onStart() {
            GuliLog.tracePrintInfo("Chart.StatisticChartView", "Scroll OnStart!!");
            this.f3896a.f3851i = true;
        }

        @SuppressLint({"NewApi"})
        public boolean onScrollX(int i) {
            if (this.f3896a.f3919G == 1) {
                i = -i;
            }
            ((StatisticChart) this.f3896a.f3847e).scroll((float) i);
            this.f3896a.postInvalidateOnAnimation();
            StatisticChartView.m8520b(this.f3896a, i);
            return true;
        }

        public void onJustify() {
            GuliLog.tracePrintInfo("Chart.StatisticChartView", "Scroll OnJustify!!");
            if (Math.abs(this.f3896a.f3852j) > 1) {
                int c = ((StatisticChart) this.f3896a.f3847e).justified();
                if (this.f3896a.f3919G == 1) {
                    c = -c;
                }
                this.f3896a.f3850h.scrollX(c);
            }
        }

        public boolean onScrollY(int i) {
            return true;
        }

        public void onFinish() {
            GuliLog.tracePrintInfo("Chart.StatisticChartView", "Scroll OnFinish!!");
            this.f3896a.f3848f.onToItem(((StatisticChart) this.f3896a.f3847e).itemOffset());
            if (this.f3896a.f3851i) {
                this.f3896a.f3851i = false;
            }
            this.f3896a.f3852j = 0;
        }
    }

    class C0732I implements ChartDataLoader.LoadCallback {
        final /* synthetic */ StatisticChartView f3894a;

        C0732I(StatisticChartView statisticChartView) {
            this.f3894a = statisticChartView;
        }

        public void onDataLoaded(ItemData c0737e) // m8477a
        {
            if (c0737e != null) {
                if (!this.f3894a.m8531h(c0737e.f3897g)) {
                    this.f3894a.m8516a((C0738L) c0737e);
                }
                // TBD HACK here to remove by IT, which can avoid error
                this.f3894a.f3929y.remove(Integer.valueOf(c0737e.f3897g));

                if (this.f3894a.f3929y.isEmpty()) {
                    if (this.f3894a.f3913A) {
                        onToItem(this.f3894a.f3928x);
                    }
                    ((StatisticChart) this.f3894a.f3847e).notifyChanged();
                    this.f3894a.f3930z.onDataLoaded(c0737e);
                }
            }
        }

        public boolean hasData(int i) { //m8478a
            return this.f3894a.f3930z.hasData(i);
        }

        public ItemData loadData(int i) {
            ItemData b = this.f3894a.f3848f.getItemData(i);
            return b == null ? this.f3894a.f3930z.loadData(i) : b;
        } //m8479b

        public void onToItem(int i) {
            this.f3894a.f3930z.onToItem(i);
        }
    }

    class C0734J implements C0733M {
        final /* synthetic */ StatisticChartView f3895a;

        C0734J(StatisticChartView statisticChartView) {
            this.f3895a = statisticChartView;
        }

        public void m8481a() {
            this.f3895a.f3850h.forceFinishScrolling();
        }

        public void onDataLoaded(ItemData c0737e) {
        }

        public boolean hasData(int i) {
            return this.f3895a.f3930z.hasData(i);
        }

        public ItemData loadData(int i) {
            this.f3895a.m8555d(i);
            this.f3895a.f3913A = false;
            return null;
        }

        public void onToItem(int i) {
            this.f3895a.f3930z.onToItem(i);
        }
    }


    public StatisticChartView(Context context) {
        this(context, null);
    }

    public StatisticChartView(Context context, AttributeSet attributeSet) {
        this(context, attributeSet, f3908a);
    }

    public StatisticChartView(Context context, AttributeSet attributeSet, int i) {
        super(context, attributeSet, i);
        this.f3920H = false;
        this.f3921I = new C0736K(this);
        this.f3922r = new C0759p(f3908a);
        this.f3923s = new C0759p(f3908a);
        this.f3847e = new StatisticChart(context);
        this.f3855m = new AccelerateDecelerateInterpolator();
        this.f3924t = new ArrayList();
        this.f3925u = new ArrayList();
        this.f3926v = new ArrayList();
        this.f3927w = new ArrayList();
        this.f3850h = new ChartScroller(getContext(), this.f3921I);
        this.f3929y = new ArrayList();
    }

    private void m8516a(C0738L c0738l) {
        if (this.f3918F == PersonInfo.INCOMING_CALL_DISABLE_BIT) {
            C0730H c0730h = new C0730H(getContext());
            c0730h.f3883b = (int) (c0738l.f3902e * 10.0f);
            c0730h.f3884c = c0738l.f3897g;
            c0730h.setRenderer(this.f3922r);
            c0730h.setParent(this.f3847e);
            this.f3926v.add(c0730h);
        } else {
            StatisticChart.StatisticBarItem c0727e = new StatisticChart(getContext()).new StatisticBarItem(getContext());
            c0727e.f3883b = c0738l.f3898a;
            c0727e.f3887a = c0738l.f3899b;
            c0727e.f3884c = c0738l.f3897g;
            c0727e.setRenderer(this.f3922r);
            c0727e.setParent(this.f3847e);
            this.f3924t.add(c0727e);
            c0727e = new StatisticChart(getContext()).new StatisticBarItem(getContext());
            c0727e.f3883b = c0738l.f3900c;
            c0727e.f3887a = c0738l.f3901d;
            c0727e.f3884c = c0738l.f3897g;
            c0727e.setRenderer(this.f3923s);
            c0727e.setParent(this.f3847e);
            this.f3925u.add(c0727e);
        }
        StatisticChart.DateItem c0721b = new StatisticChart(getContext()).new DateItem();
        c0721b.f3837a = c0738l.f3897g;
        c0721b.f3838b = c0738l.f3903f;
        this.f3927w.add(c0721b);
        m8538n();
    }

    static /* synthetic */ int m8520b(StatisticChartView statisticChartView, int i) {
        int i2 = statisticChartView.f3852j + i;
        statisticChartView.f3852j = i2;
        return i2;
    }

    private boolean m8531h(int i) {

        switch (this.f3918F) {
            case f3909b /*1*/:
                List<StatisticChart.StatisticBarItem> lista = this.f3924t;
                for (StatisticChart.StatisticBarItem c0726c : lista) {
                    if (c0726c.f3884c == i) {
                        return true;
                    }
                }

                break;
            case C4220a.bp /*16*/:
                List<StatisticChart.StatisticBarItem> listb = this.f3925u;
                for (StatisticChart.StatisticBarItem c0726c : listb) {
                    if (c0726c.f3884c == i) {
                        return true;
                    }
                }
                break;
            case PersonInfo.INCOMING_CALL_DISABLE_BIT /*256*/:
                List<C0730H> listc = this.f3926v;
                for (C0730H c0726c : listc) {
                    if (c0726c.f3884c == i) {
                        return true;
                    }
                }
                break;
        }


        return false;
    }

    private void m8538n() {
        if (this.f3918F == PersonInfo.INCOMING_CALL_DISABLE_BIT) {
            Collections.sort(this.f3926v);
            ((StatisticChart) this.f3847e).fillWeightData(this.f3926v);
        } else {
            ((StatisticChart) this.f3847e).fillStepData(this.f3924t);
            ((StatisticChart) this.f3847e).fillSleepData(this.f3925u);
        }
        ((StatisticChart) this.f3847e).fillDates(this.f3927w);
    }

    public Animator m8540a() {
        this.f3914B = -1;
        this.f3915C = -1;
        this.f3916D = -1;
        this.f3917E = -1;
        return super.animRefresh();
    }

    public Animator m8541a(float f, float f2) {
        this.f3914B = -1;
        this.f3915C = f;
        this.f3916D = -1;
        this.f3917E = f2;
        return super.animRefresh();
    }

    public Animator m8542a(long j, float f, float f2) {
        this.f3914B = f;
        this.f3915C = -1;
        this.f3916D = f2;
        this.f3917E = -1;
        return super.animRefresh(j);
    }

    public BarItem m8543a(int i, int i2) {
        switch (this.f3918F) {
            case f3909b /*1*/:
                List<StatisticChart.StatisticBarItem> lista = this.f3924t;
                for (StatisticChart.StatisticBarItem c0726c : lista) {
                    if (c0726c.f3884c == i2) {
                        return c0726c;
                    }
                }

                break;
            case C4220a.bp /*16*/:
                List<StatisticChart.StatisticBarItem> listb = this.f3925u;
                for (StatisticChart.StatisticBarItem c0726c : listb) {
                    if (c0726c.f3884c == i2) {
                        return c0726c;
                    }
                }
                break;
            case PersonInfo.INCOMING_CALL_DISABLE_BIT /*256*/:
                List<C0730H> listc = this.f3926v;
                for (C0730H c0726c : listc) {
                    if (c0726c.f3884c == i2) {
                        return c0726c;
                    }
                }
                break;
        }

        return null;
    }

    public void m8544a(float f) {
        ((StatisticChart) this.f3847e).setWeightBase((int) (10.0f * f));
    }

    public void m8545a(int i) {
        this.f3918F = i;
        ((StatisticChart) this.f3847e).m8377e(i);
    }

    public void m8546a(ChartDataLoader c0746d) {
        this.f3930z = c0746d.getLoadCallback();
        this.f3848f = c0746d;
        this.f3848f.setLoadCallback(new C0732I(this));
        ((StatisticChart) this.f3847e).setLoadCallback(new C0734J(this));
    }

    public Animator m8547b(long j, float f, float f2) {
        this.f3914B = -1;
        this.f3915C = f;
        this.f3916D = -1;
        this.f3917E = f2;
        return super.animRefresh(j);
    }

    public void m8548b() {
        this.f3914B = -1;
        this.f3915C = -1;
        this.f3916D = -1;
        this.f3917E = -1;
    }

    public void m8549b(float f) {
        ((StatisticChart) this.f3847e).setWeightGoal((int) (10.0f * f));
    }

    public void m8550b(int i) {
        this.f3919G = i;
        ((StatisticChart) this.f3847e).m8380f(i);
    }

    public void m8551b(boolean z) {
        this.f3920H = z;
    }

    public void m8552c() {
        this.f3914B = -1;
        this.f3915C = -1;
        this.f3916D = -1;
        this.f3917E = -1;
        super.refresh();
    }

    public void m8553c(int i) {
        ((StatisticChart) this.f3847e).updateBarItemCount(i);
    }

    public float m8554d() {
        return ((StatisticChart) this.f3847e).getBarItemWidth();
    }

    public void m8555d(int i) {
        this.f3928x = i;
        this.f3913A = true;
        int a = ((((StatisticChart) this.f3847e).m8361a() + f3909b) / f3910c) - 1;
        int i2 = -1;
        int a2 = ((StatisticChart) this.f3847e).m8361a();
        if (this.f3918F == PersonInfo.INCOMING_CALL_DISABLE_BIT) {
            i2 = -3;
            a2 += f3910c;
        }
        while (i2 <= a2) {
            int i3 = (i - i2) + a;
            if (!this.f3929y.contains(Integer.valueOf(i3)) && this.f3930z.hasData(i3)) {
                this.f3929y.add(Integer.valueOf(i3));
            }
            i2 += f3909b;
        }
        if (this.f3929y.size() > 0) {
            Iterator it = this.f3929y.iterator();
            while (it.hasNext()) {
                this.f3848f.loadItemData(((Integer) it.next()).intValue());
                it = this.f3929y.iterator();
            }
        }
    }

    public void m8556e() {
        this.f3925u.clear();
        this.f3924t.clear();
        this.f3926v.clear();
        this.f3927w.clear();
        m8538n();
        this.f3929y.clear();
        ((StatisticChart) this.f3847e).clearData();
        ((StatisticChart) this.f3847e).notifyChanged();
    }

    public void m8557e(int i) {
        ((StatisticChart) this.f3847e).setStepGoal(i);
    }

    public void m8558f() {
        int b = (int) ((StatisticChart) this.f3847e).getBarItemWidth();
        if (this.f3919G == f3909b) {
            b = -b;
        }
        this.f3850h.scrollX(b);
    }

    public void m8559f(int i) {
        ((StatisticChart) this.f3847e).offsetTo(-i);
    }

    public void m8560g() {
        int i = -((int) ((StatisticChart) this.f3847e).getBarItemWidth());
        if (this.f3919G == f3909b) {
            i = -i;
        }
        this.f3850h.scrollX(i);
    }

    public void m8561g(int i) {
        int i2 = -((int) (((StatisticChart) this.f3847e).getBarItemWidth() * ((float) i)));
        if (this.f3919G == f3909b) {
            i2 = -i2;
        }
        this.f3850h.scrollX(i2);
    }

    protected void onDraw(Canvas canvas) {
        ((StatisticChart) this.f3847e).draw(canvas, this.f3857o, this.f3914B, this.f3915C, this.f3916D, this.f3917E, this.f3854l);
    }

    public boolean onTouchEvent(MotionEvent motionEvent) {
        if (!this.f3849g) {
            return false;
        }
        if (!(getParent() == null || this.f3920H)) {
            getParent().requestDisallowInterceptTouchEvent(true);
        }
        switch (motionEvent.getAction()) {
            case f3909b /*1*/:
                if (!this.f3851i) {
                    this.f3850h.scrollX(((StatisticChart) this.f3847e).offsetScrolled(motionEvent.getX()));
                    break;
                }
                break;
        }
        return this.f3850h.onTouchEvent(motionEvent);
    }
}
